package com.banzhi.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.RefundRequest.Refund_Data;
import com.banzhi.shop.entity.ShouhouRequest.Request_Data;
import com.banzhi.shop.model.CallBack.CancelShouhouOrderCallBack;
import com.banzhi.shop.model.CallBack.HuanhuoTrueCallBack;
import com.banzhi.shop.model.CallBack.RefundRequestCallBack;
import com.banzhi.shop.model.CallBack.ShouhouRequestCallBack;
import com.banzhi.shop.model.ICancelShouhouOrderModel;
import com.banzhi.shop.model.IHuanhuoTrueModel;
import com.banzhi.shop.model.IRefundRequestModel;
import com.banzhi.shop.model.IShouhouRequestModel;
import com.banzhi.shop.model.Impl.CancelShouhouOrderImpl;
import com.banzhi.shop.model.Impl.HuanhuoTrueImpl;
import com.banzhi.shop.model.Impl.RefundRequestImpl;
import com.banzhi.shop.model.Impl.ShouhouRequestImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.DateUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

/**
 * 售后申请页面
 */
public class ShouhouShenqingActivity extends BaseActivity implements View.OnClickListener, ShouhouRequestCallBack, RefundRequestCallBack, CancelShouhouOrderCallBack, HuanhuoTrueCallBack, SwipeRefreshLayout.OnRefreshListener {
    private ImageView ivback;
    /**
     * 传过来的售后ID
     */
    private String refundid;
    /**
     * 订单ID
     */
    private String id;
    private IShouhouRequestModel model;
    private Request_Data data;
    /**
     * 三种状态 不同的状态显示还是隐藏
     */
//    private LinearLayout lltype;
    private TextView tvliucheng;
    private TextView tvchulifangshi;
    private TextView tvreson;
    private TextView tvshuoming;
    private TextView tvmoney;
    private TextView tvtime;
    private TextView tvtuiaddress;
    private TextView tvtuiname;
    private TextView tvtuiphone;
    private TextView tvliuyan;
    private TextView tvstatu;
    private TextView tvbottom;
    private RelativeLayout llbom;
    /**
     * 申请退款的接口
     */
    private IRefundRequestModel requestModel;
    private LinearLayout lldizhi;
    private LinearLayout llliuyan;
    private TextView tvdanhao;
    private TextView tvfuzhi;
    private TextView tv_cancel;
    private LinearLayout llkuaidi;
    private TextView tv_kuaidigongsi;
    private TextView tv_kuaidinum;
    private TextView fuzhi3;
    /**
     * 取消售后申请
     */
    private ICancelShouhouOrderModel cancelShouhouOrderModel;
    private IHuanhuoTrueModel huanhuoTrueModel;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouhou_shenqing);
        Translucent.setTranslucent(this);
        initsView();
        setListeners();
        Intent intent = getIntent();
        refundid = intent.getStringExtra("refundid");
        id = intent.getStringExtra("id");
        model = new ShouhouRequestImpl();

        requestModel = new RefundRequestImpl();
        cancelShouhouOrderModel = new CancelShouhouOrderImpl();
        huanhuoTrueModel = new HuanhuoTrueImpl();

    }

    @Override
    protected void onStart() {
        super.onStart();
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.request(refundid, this);
    }

    private void initsView() {
        ivback = (ImageView) findViewById(R.id.iv_back);
//        lltype = (LinearLayout) findViewById(R.id.ll_type);
        tvliucheng = (TextView) findViewById(R.id.tv_liucheng);
        tvchulifangshi = (TextView) findViewById(R.id.tv_chulifangshi);
        tvreson = (TextView) findViewById(R.id.tv_reson);
        tvshuoming = (TextView) findViewById(R.id.tv_shuoming);
        tvmoney = (TextView) findViewById(R.id.tv_money);
        tvtime = (TextView) findViewById(R.id.tv_time);
        tvtuiaddress = (TextView) findViewById(R.id.tv_tuiaddress);
        tvtuiname = (TextView) findViewById(R.id.tv_tuiname);
        tvtuiphone = (TextView) findViewById(R.id.tv_tuiphone);
        tvliuyan = (TextView) findViewById(R.id.tv_liuyan);
        tvstatu = (TextView) findViewById(R.id.tv_statu);
        tvbottom = (TextView) findViewById(R.id.tv_bottom);
        llbom = (RelativeLayout) findViewById(R.id.ll_bom);
        lldizhi = (LinearLayout) findViewById(R.id.ll_dizhi);
        llliuyan = (LinearLayout) findViewById(R.id.ll_liuyan);
        tvdanhao = (TextView) findViewById(R.id.tv_danhao);
        tvfuzhi = (TextView) findViewById(R.id.tv_fuzhi);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_kuaidigongsi = (TextView) findViewById(R.id.tv_kuaidigongsi);
        tv_kuaidinum = (TextView) findViewById(R.id.tv_kuaidiNum);
        fuzhi3 = (TextView) findViewById(R.id.tv_fuzhi3);
        llkuaidi = (LinearLayout) findViewById(R.id.ll_kuaidi);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.srl_listview);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        swipeRefreshLayout.setColorSchemeResources(R.color.themeColor);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        tvbottom.setOnClickListener(this);
        tvfuzhi.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        fuzhi3.setOnClickListener(this);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_bottom://填写快递单号  缺少值来判断此时是填写还是修改
                if ("0".equals(data.getRefund().getRefundStatus())) {
                    LoadingDialogUtils.showProgress(ShouhouShenqingActivity.this, "正在加载,请稍后~");
                    requestModel.getData(id, this);
                } else if ("3".equals(data.getRefund().getRefundStatus())) {
                    Intent SWintent = new Intent(this, ShouhouWuliuActivity.class);
                    SWintent.putExtra("refundid", refundid);
                    startActivity(SWintent);
                } else if ("4".equals(data.getRefund().getRefundStatus())) {
                    Intent SWintent = new Intent(this, ShouhouWuliuActivity.class);
                    SWintent.putExtra("refundid", refundid);
                    startActivity(SWintent);
                } else if ("5".equals(data.getRefund().getRefundStatus())) {
                    showquerenPopupwindow(refundid);

                }
                break;
            case R.id.tv_fuzhi:
                ShareUtils.copyLianjie(this, tvdanhao.getText().toString().trim());
                break;
            case R.id.tv_cancel:
                showPopupwindow(refundid);

                break;
            case R.id.tv_fuzhi3:
                ShareUtils.copyLianjie(this, tv_kuaidinum.getText().toString().trim());
                break;
        }
    }

    /**
     * 获取售后信息成功
     *
     * @param object
     */
    @Override
    public void requestSuccess(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        data = (Request_Data) object;
        tvdanhao.setText(data.getRefund().getRefundno());
        tvliucheng.setText(data.getDescription());
        if ("0".equals(data.getRefund().getRtype())) {
            tvchulifangshi.setText("退款");
        } else if ("1".equals(data.getRefund().getRtype())) {
            tvchulifangshi.setText("退货退款");
        } else if ("2".equals(data.getRefund().getRtype())) {
            tvchulifangshi.setText("换货");
        }
        tvreson.setText(data.getRefund().getReason());
        tvshuoming.setText(data.getRefund().getContent());
        if (ASUtils.isCanUse(data.getRefund().getApplyprice())) {
            tvmoney.setText(NumberUtils.DecimalFormat(data.getRefund().getApplyprice()));
        }
        if (ASUtils.isCanUse(data.getRefund().getCreatetime())) {
            tvtime.setText(DateUtil.getData(data.getRefund().getCreatetime()));
        }

        if (ASUtils.isCanUse(data.getAddress().getAddress())) {
            tvtuiaddress.setText(data.getAddress().getAddress());
        }
        if (ASUtils.isCanUse(data.getAddress().getName())) {
            tvtuiname.setText(data.getAddress().getName());
        }
        if (ASUtils.isCanUse(data.getAddress().getMobile())) {
            tvtuiphone.setText(data.getAddress().getMobile());
        }
        if (ASUtils.isCanUse(data.getRefund().getMessage())) {
            tvliuyan.setText(data.getRefund().getMessage());
        }
//        if ("0".equals(data.getRefund().getStatus())) {
//            lltype.setVisibility(View.GONE);
//            tvstatu.setText("等待商家处理售后申请");
//            tvbottom.setText("修改申请");
//        } else if ("3".equals(data.getRefund().getStatus())) {
//            lltype.setVisibility(View.VISIBLE);
//            tvstatu.setText("商家已经通过售后申请");
//            tvbottom.setText("填写快递单号");
//        } else if ("4".equals(data.getRefund().getStatus())) {
//            lltype.setVisibility(View.VISIBLE);
//            tvstatu.setText("商家已经通过售后申请");
//            tvbottom.setText("修改快递单号");
//        }
        if ("-2".equals(data.getRefund().getRefundStatus())) {
            tvstatu.setText("售后申请已取消");
        } else if ("-1".equals(data.getRefund().getRefundStatus())) {
            tvstatu.setText("售后申请被拒绝");
        } else if ("0".equals(data.getRefund().getRefundStatus())) {
            llbom.setVisibility(View.VISIBLE);
            tv_cancel.setVisibility(View.VISIBLE);
            tvstatu.setText("等待商家处理售后申请");
            tvbottom.setText("修改申请");
        } else if ("1".equals(data.getRefund().getRefundStatus())) {
            if (!"0".equals(data.getRefund().getRtype())) {
                lldizhi.setVisibility(View.VISIBLE);
                llliuyan.setVisibility(View.VISIBLE);
            }
            tvstatu.setText("售后申请已完成");
        } else if ("3".equals(data.getRefund().getRefundStatus())) {
            lldizhi.setVisibility(View.VISIBLE);
            llliuyan.setVisibility(View.VISIBLE);
            llbom.setVisibility(View.VISIBLE);
            tv_cancel.setVisibility(View.VISIBLE);
            tvstatu.setText("商家已经通过售后申请");
            tvbottom.setText("填写快递单号");
        } else if ("4".equals(data.getRefund().getRefundStatus())) {
            lldizhi.setVisibility(View.VISIBLE);
            llliuyan.setVisibility(View.VISIBLE);
            llbom.setVisibility(View.VISIBLE);
            tvstatu.setText("商家已经通过售后申请");
            tvbottom.setText("修改快递单号");
        } else if ("5".equals(data.getRefund().getRefundStatus())) {
            lldizhi.setVisibility(View.VISIBLE);
            llliuyan.setVisibility(View.VISIBLE);
            llbom.setVisibility(View.VISIBLE);
            tvstatu.setText("卖家已发货");
            tvbottom.setText("确认收货");
        }
        if (ASUtils.isCanUse(data.getRefund().getRexpresscom())) {
            llkuaidi.setVisibility(View.VISIBLE);
            tv_kuaidigongsi.setText(data.getRefund().getRexpresscom());
            tv_kuaidinum.setText(data.getRefund().getRexpresssn());
        }
    }

    /**
     * 获取售后信息失败
     *
     * @param object
     */
    @Override
    public void requestError(Object object) {
        if (swipeRefreshLayout.isRefreshing()) {
            swipeRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 申请退款成功
     *
     * @param object
     */
    @Override
    public void onrefundrequestSuccess(Object object) {
        Refund_Data refund = (Refund_Data) object;
        Intent RRintent = new Intent(this, RefundRequestActivity.class);
        RRintent.putExtra("data", refund);
        RRintent.putExtra("Orderid", id);
        RRintent.putExtra("Refunddata", data.getRefund());
        startActivity(RRintent);

    }

    /**
     * 申请退款失败
     *
     * @param object
     */
    @Override
    public void onrefundrequestError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 取消申请成功
     *
     * @param object
     */
    @Override
    public void CancelOrderSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        ToastUtils.makeToastShort("取消售后成功");
        finish();
    }

    /**
     * 取消申请失败
     *
     * @param object
     */
    @Override
    public void CancelOrderError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 换货确认收货成功
     *
     * @param object
     */
    @Override
    public void huanSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        finish();
    }

    /**
     * 换货失败
     *
     * @param object
     */
    @Override
    public void huanError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }


    /**
     * 点击取消订单
     */
    private void showPopupwindow(final String refundid) {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_cancel_shouhou, null);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoadingDialogUtils.showProgress(ShouhouShenqingActivity.this, "正在加载,请稍后~");
                cancelShouhouOrderModel.CancelOrder(refundid, ShouhouShenqingActivity.this);
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.request(refundid, this);
    }

    /**
     * 点击确认收货是不是确定确认收货
     *
     * @param id
     */
    private void showquerenPopupwindow(final String id) {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_queren_shouhuo, null);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoadingDialogUtils.showProgress(ShouhouShenqingActivity.this, "正在加载,请稍后~");
                huanhuoTrueModel.getData(refundid, ShouhouShenqingActivity.this);
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
